import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-04-05
 * Time: 9:26
 */
public class Test2 {

    public static int longestArithSeqLength(int[] nums) {
        // 创建dp表、初始化、填表、返回值
        int n = nums.length;
        int[][] dp = new int[n][n];

        for (int i = 0; i < n; i++) {
            Arrays.fill(dp[i], 2);
        }

        // 绑定 原始数组的值 和 下标数组的关系
        Map<Integer, List<Integer>> hash = new HashMap<>();
        for (int i = 0; i < n; i++) {
            List<Integer> list = hash.get(nums[i]);
            if (list == null) {
                list = new LinkedList<>();
            }
            list.add(i);
            hash.put(nums[i], list);
        }

        int ret = 2;
        for (int i = 2; i < n; i++) {
            for (int j = 1; j < i; j++) {
                // 确定了dp[i][j]的值
                int a = 2 * nums[j] - nums[i];
                // 寻找离b最近的a
                List<Integer> list = hash.get(a);
                if (list != null) {
                    int index = -1;
                    for (int x : list) {
                        // 符合要求且离b最近
                        if (x < j && x > index) {
                            index = x;
                        }
                    }
                    if (index != -1) {
                        dp[j][i] = dp[index][j] + 1;
                    }
                }
                ret = Math.max(ret, dp[j][i]);
            }
        }

        return ret; // 这里不需要判断是否等于2
    }


    public static void main(String[] args) {
        longestArithSeqLength(new int[]{0, 250, 500});
    }
}